package com.ifast.asset.controller;


import java.sql.Array;
import java.util.*;

import com.ifast.asset.domain.Assets;
import com.ifast.asset.domain.LabelDO;
import com.ifast.asset.domain.LabelassetDO;
import com.ifast.asset.service.AssetsService;
import com.ifast.asset.service.LabelService;
import com.ifast.asset.service.LabelassetService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ifast.common.annotation.Log;
import com.ifast.common.base.BaseController;
import com.ifast.common.utils.Result;

/**
 * 
 * <pre>
 * 
 * </pre>
 * <small> 2021-03-09 11:02:24 | Aron</small>
 */
@Controller
@RequestMapping("/wxmp/labelasset")
public class LabelassetController extends BaseController {
	@Autowired
	private LabelassetService labelassetService;
	@Autowired
	private AssetsService assetsService;
	@Autowired
	private LabelService labelService;

	@GetMapping()
	@RequiresPermissions("wxmp:labelasset:labelasset")
	String Labelasset(){
	    return "wxmp/labelasset/labelasset";
	}
	
	@ResponseBody
	@GetMapping("/list")
	//@RequiresPermissions("wxmp:labelasset:labelasset")
	public Result<Page<LabelassetDO>> list(LabelassetDO labelassetDTO){
        Wrapper<LabelassetDO> wrapper = new EntityWrapper<>(labelassetDTO).orderBy("id", false);
        Page<LabelassetDO> page = labelassetService.selectPage(getPage(LabelassetDO.class), wrapper);
        return Result.ok(page);
	}
	
	@GetMapping("/add")
	//@RequiresPermissions("wxmp:labelasset:add")
	String add(){
	    return "wxmp/labelasset/add";
	}

	@GetMapping("/edit/{id}")
	//@RequiresPermissions("wxmp:labelasset:edit")
	String edit(@PathVariable("id") Integer id,Model model){
		LabelassetDO labelasset = labelassetService.selectById(id);
		model.addAttribute("labelasset", labelasset);
	    return "wxmp/labelasset/edit";
	}
	
	@Log("添加")
	@ResponseBody
	@PostMapping("/save")
	//@RequiresPermissions("wxmp:labelasset:add")
	public Result<String> save( LabelassetDO labelasset){
		labelassetService.insert(labelasset);
        return Result.ok();
	}
	
	@Log("修改")
	@ResponseBody
	@RequestMapping("/update")
	//@RequiresPermissions("wxmp:labelasset:edit")
	public Result<String>  update( LabelassetDO labelasset){
		boolean update = labelassetService.updateById(labelasset);
		return update ? Result.ok() : Result.fail();
	}
	
	@Log("删除")
	@PostMapping( "/remove")
	@ResponseBody
	//@RequiresPermissions("wxmp:labelasset:remove")
	public Result<String>  remove( Integer id){
		labelassetService.deleteById(id);
        return Result.ok();
	}
	
	@Log("批量删除")
	@PostMapping( "/batchRemove")
	@ResponseBody
	//@RequiresPermissions("wxmp:labelasset:batchRemove")
	public Result<String>  remove(@RequestParam("ids[]") Integer[] ids){
		labelassetService.deleteBatchIds(Arrays.asList(ids));
		return Result.ok();
	}

	@ResponseBody
	@GetMapping("/labelAndAssetslist")
	//@RequiresPermissions("wxmp:labelasset:labelasset")
	public Result<Object> labelAndAssetslist(String[] labels, Integer pageNum, Integer pageSize){

		List<Map<String , Object>> mapList = new ArrayList<>();
		//多个标签筛选  查询两次   第一次获取lableasset表的分页之后数据  ，目的时查询出assetid ，第二次查询 为了查询出所有信息
		Wrapper wrapper = new EntityWrapper();
		for (int i =  0 ; i < labels.length ; i++){

			if (i==0){
				wrapper.eq("labelId",labels[i]);
			}else {
				wrapper.or().eq("labelId",labels[i]);
			}
		}
		wrapper.groupBy("assetId");
		Page labelassets = labelassetService.selectPage(getPage(LabelassetDO.class,pageNum,pageSize) , wrapper);
		List<LabelassetDO> lists = labelassets.getRecords();
		//解析  分页数据  查询此页数据的详细信息  并返回
		for (int i = 0 ; i < lists.size() ; i++){
			Map<String , Object> map = new HashMap<>();
			int assetid = lists.get(i).getAssetId();
			Assets asset = assetsService.selectById(assetid);
			//查询 资产对应标签信息
			Wrapper labelwrapper = new EntityWrapper();
			labelwrapper.eq("assetId",asset.getId());
			List<LabelassetDO> labelassetlist = labelassetService.selectList(labelwrapper);
			List<String> lablelists = new ArrayList<>();
			for (int j = 0 ; j < labelassetlist.size() ; j++){

				LabelDO labelDO = labelService.selectById(labelassetlist.get(j).getLabelId());
				lablelists.add(labelDO.getName());
			}
			//添加数据
			map.put("lable",lablelists);
			map.put("id",asset.getId());
			map.put("ip",asset.getIp());
			map.put("assetsName",asset.getAssetsName());
			mapList.add(map);
		}
		labelassets.setRecords(mapList);
		return Result.ok(labelassets);
	}
}
